﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Fis.Web.Rules.Entry
{
    public class VersionUpdate
    {
        /// <summary>
        /// 指定配置的SID
        /// </summary>
        public readonly static string DefaultConfig = "_default_version_";

        /// <summary>
        /// 数据库配置信息
        /// </summary>
        public static System.Data.DataTable Config
        {
            get
            {
                var dc = Agile.Engine.Data.DataContext.Get("ServiceDataContext");
                return dc.DefaultModel.QueryTable("TXTRA", "SID='" + DefaultConfig + "'");
            }
        }

        System.Data.DataTable dataConfig = Config;

        public bool HasNewVersion(string varsion)
        {
            return !varsion.Equals(dataConfig.Rows[0]["FXTRA2"].ToString());
        }

        /// <summary>
        /// 新版本包信息
        /// </summary>
        public System.IO.Stream NewVersion
        {
            get
            {
                var source = System.IO.Path.Combine(dataConfig.Rows[0]["FXTRA1"].ToString(), "update.xap");
                return System.IO.File.OpenRead(source);
            }
            set
            {
                var source = System.IO.Path.Combine(Config.Rows[0]["FXTRA1"].ToString(), "update.xap");
                Agile.Engine.Utils.IOUtils.CreateDirectory(source);

                using (var f = System.IO.File.OpenWrite(source))
                {
                    value.CopyTo(f);
                }
            }
        }

    }
}
